﻿using Learun.Util.SqlSugar;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Text;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-03-14 10:56
    /// 描 述：对象类型元件库
    /// </summary>
    public class ec_objecttypelibService
    {
        #region 仓储
        Repository<ec_projectEntity> _projectRepository => new Repository<ec_projectEntity>();
        Repository<ec_objecttypelibEntity> _objecttypelibRepository => new Repository<ec_objecttypelibEntity>();
        #endregion
        #region 获取数据

        /// <summary>
        /// 获取对象类型元件库表数据
        /// <summary>
        /// <param name="objectTypeID">对象类型ID</param>
        /// <param name="ProjectId">项目ID</param>
        /// <returns></returns>
        public IEnumerable<ec_objecttypelibEntity> GetObjectTypeLib(string objectTypeID, string ProjectId)
        {
            try
            {
                var strSql = new StringBuilder();
                strSql.Append(@" SELECT t1.* ");
                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    strSql.Append($" FROM ec_objecttypelib t1 ");
                }
                else
                {
                    //var ProjectEntity = this.BaseRepository().FindEntity<ec_projectEntity>(ProjectId);
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    strSql.Append($" FROM ec_objecttypelib_{ProjectEntity.ProjectIndex} t1 ");
                }
                if (!string.IsNullOrEmpty(objectTypeID))
                {
                    strSql.Append($" WHERE ObjectTypeID = '{objectTypeID}' ");
                }
                //return this.BaseRepository().FindList<ec_objecttypelibEntity>(strSql.ToString());
                return SqlSugarHelper.Db.SqlQueryable<ec_objecttypelibEntity>(strSql.ToString()).ToList();
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        #endregion

        #region 提交数据

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveObjectTypeLib(string objectTypeID, List<ec_objecttypelibEntity> entityList, string ProjectId)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    _objecttypelibRepository.Delete(t => t.ObjectTypeID == objectTypeID);
                    _objecttypelibRepository.InsertRange(entityList);
                }
                else
                {
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_objecttypelib_{ ProjectEntity.ProjectIndex} WHERE ObjectTypeID = @ObjectTypeID  ", new { ObjectTypeID = objectTypeID });
                    //db.InsertByNoMap(entityList, $"ec_objecttypelib_{ ProjectEntity.ProjectIndex}");
                    _objecttypelibRepository.AsInsertable(entityList).AS($"ec_objecttypelib_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                }
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

    }
}
